<?php
namespace app\api\controller;
use app\common\BaseController;
use think\Db;
use app\common\Logs;

class Report extends BaseController{    
    public function __construct(){
        parent::__construct();       
    }
    public function index(){         
        $where = [];        
        $params = $this->request->param();          
        $date=[];        

        if(!empty($params['start_date']) && !empty($params['end_date'])){
            $date=[                
                'start_date'=>$params['start_date'],
                'end_date'=>$params['end_date']
            ];
        }else{
            return "缺少日期";
        }  
        if(!empty($params['is_departure']) && $params['is_departure']==1){
            $date['departure']=$params['is_departure'];
        }else{
            $date['departure']=0;
        }
        

        $list = Db::query('select id,title from lt_category where parent_id=9 and id not in (32,34)');
        array_push($list,['id'=>0,'title'=>"系统对接"]);
        
        $data=[];        
        foreach($list as $item){       
            $tmp=[];     
            //单项     
            
            if($params['type']==1){
                $tmp=$this->team(1,$date,$item);               
            }elseif($params['type']==2){
                $tmp=$this->team(2,$date,$item);
            }elseif($params['type']==3){
                $tmp=$this->team(3,$date,$item);
            }elseif($params['type']==4){
                $tmp=$this->single(4,$date,$item);
            }elseif($params['type']==5){
                $tmp=$this->single(5,$date,$item);
            }elseif($params['type']==8){
                $tmp=$this->single(8,$date,$item);
            }   
            elseif($params['type']==12){
                $tmp=$this->single(12,$date,$item);
            }elseif($params['type']==16){
                $tmp=$this->team(16,$date,$item);
            }elseif($params['type']==17){
                $tmp=$this->team(17,$date,$item);
            }
            $tmp['branch']=$item['title']; 
            array_push($data,$tmp);   
        }       
        return $data;     
    }

    public function Summary(){         
        $where = [];        
        $params = $this->request->param();          
        $date=[];        

        if(!empty($params['start_date']) && !empty($params['end_date'])){
            $date=[                
                'start_date'=>$params['start_date'],
                'end_date'=>$params['end_date']
            ];
        }else{
            return "缺少日期";
        }  
        if(!empty($params['is_departure']) && $params['is_departure']==1){
            $date['departure']=$params['is_departure'];
        }else{
            $date['departure']=0;
        }
        

        $list = Db::query('select id,title from lt_category where parent_id=9 and id not in (32,34)');
        array_push($list,['id'=>0,'title'=>"系统对接"]);
        
        $data=[];        
        foreach($list as $item){       
            $tmp=[];
            if($item['id']==122){
                $cost=$this->cost(9,$date,$item); 
                $tmp['total_sell_price']=$cost['total_sell_price'];
                $tmp['received']=$cost['received'];
                $tmp['no_received']=$cost['no_received'];
                $tmp['total_cost_price']=$cost['total_cost_price'];
                $tmp['payment']=$cost['payment'];
                $tmp['no_payment']=$cost['no_payment'];
                $tmp['profit']=$cost['profit'];                
                $tmp['branch']="综合费用"; 
            }elseif($item['id']==114){
                $yan=$this->yan(14,$date,$item);   
                $tmp['total_sell_price']=$yan['total_sell_price'];
                $tmp['received']=$yan['received'];
                $tmp['no_received']=$yan['no_received'];
                $tmp['total_cost_price']=$yan['total_cost_price'];
                $tmp['payment']=$yan['payment'];
                $tmp['no_payment']=$yan['no_payment'];
                $tmp['profit']=$yan['profit'];                
                $tmp['branch']="烟"; 
            }else{            
            $team=$this->team(1,$date,$item);             
            $scatter=$this->team(2,$date,$item);
            $project=$this->team(3,$date,$item);
            $single=$this->single(4,$date,$item);
            $qrs=$this->single(5,$date,$item);
            $xp=$this->single(8,$date,$item);   
            $changbud=$this->single(12,$date,$item);   
            $changbut=$this->team(16,$date,$item);
            $changbul=$this->team(17,$date,$item);

            $tmp['total_sell_price']=$team['total_sell_price']+$scatter['total_sell_price']+$project['total_sell_price']+$single['total_sell_price']+$qrs['total_sell_price']+$xp['total_sell_price']+$changbud['total_sell_price']+$changbut['total_sell_price']+$changbul['total_sell_price'];
            $tmp['received']=$team['received']+$scatter['received']+$project['received']+$single['received']+$qrs['received']+$xp['received']+$changbud['received']+$changbut['received']+$changbul['received'];
            $tmp['no_received']=$team['no_received']+$scatter['no_received']+$project['no_received']+$single['no_received']+$qrs['no_received']+$xp['no_received']+$changbud['no_received']+$changbut['no_received']+$changbul['no_received'];
            $tmp['total_cost_price']=$team['total_cost_price']+$scatter['total_cost_price']+$project['total_cost_price']+$single['total_cost_price']+$qrs['total_cost_price']+$xp['total_cost_price']+$changbud['total_cost_price']+$changbut['total_cost_price']+$changbul['total_cost_price'];
            $tmp['payment']=$team['payment']+$scatter['payment']+$project['payment']+$single['payment']+$qrs['payment']+$xp['payment']+$changbud['payment']+$changbut['payment']+$changbul['payment'];
            $tmp['no_payment']=$team['no_payment']+$scatter['no_payment']+$project['no_payment']+$single['no_payment']+$qrs['no_payment']+$xp['no_payment']+$changbud['no_payment']+$changbut['no_payment']+$changbul['no_payment'];
            $tmp['profit']=$team['profit']+$scatter['profit']+$project['profit']+$single['profit']+$qrs['profit']+$xp['profit']+$changbud['profit']+$changbut['profit']+$changbul['profit'];
            
            $tmp['branch']=$item['title']; 
            /*if($tmp['total_sell_price']&&$tmp['received']&&$tmp['no_received']&&$tmp['total_cost_price']&&$tmp['payment']&&$tmp['no_payment']&&$tmp['profit']){
                array_push($data,$tmp); 
            }*/
            }
            array_push($data,$tmp);   
        }       
        return $data;     
    }

    private function single($type,$date,$branch){
        if($date['departure']==1){
            $sql="booking_date";
        }else{
            $sql="start_date";
        }
        $single_ids_arr = Db::query("select id from lt_single where type=? and branch_id=? and {$sql}>=? and {$sql}<=?",[$type,$branch['id'],$date['start_date'],$date['end_date']]);            
        $single_ids=[];
        foreach ($single_ids_arr as $v) {
            array_push($single_ids,$v['id']);
        }
        if($single_ids){
            $single = Db::query("select sum((sell_price-pay_price)*num) as total_sell_price,sum((cost_price-pay_price)*num) as total_cost_price from lt_single where id in (".implode(',',$single_ids).")");            
            $params['total_sell_price']=floatval($single[0]['total_sell_price']?$single[0]['total_sell_price']:0);
            $params['total_cost_price']=floatval($single[0]['total_cost_price']?$single[0]['total_cost_price']:0);
            $single_receipt = Db::query("select sum(money) as received from lt_receipt where type=? and income_id in (".implode(',',$single_ids).")",[$type]);
            $params['received']=floatval($single_receipt[0]['received']?$single_receipt[0]['received']:0);
            $single_payment = Db::query("select sum(money) as payment from lt_payment where type=? and outlay_id in (".implode(',',$single_ids).")",[$type]);
            $params['payment']=floatval($single_payment[0]['payment']?$single_payment[0]['payment']:0);
            $params['no_received'] = floatval($params['total_sell_price']-$params['received']);
            $params['no_payment'] = floatval($params['total_cost_price']-$params['payment']);
            $params['profit']=floatval($params['total_sell_price']-$params['total_cost_price']);
        }else{
            $params['total_sell_price']=0;
            $params['total_cost_price']=0;              
            $params['received']=0;              
            $params['payment']=0;
            $params['no_received']=0;
            $params['no_payment']=0;
            $params['profit']=0;
        }
        return $params;
    }

    private function team($type,$date,$branch){
        if($date['departure']==1){
            $sql="create_time";
            $date['start_date']=strtotime($date['start_date']);
            $date['end_date']=strtotime($date['end_date']);
        }else{
            $sql="start_date";
        }
        //团队           
        $team_ids_arr = Db::query("select id from lt_team where type=? and branch_id=? and $sql>=? and $sql<=?",[$type,$branch['id'],$date['start_date'],$date['end_date']]);            
        $team_ids=[];
        foreach ($team_ids_arr as $v) {
            array_push($team_ids,$v['id']);
        }
        if($team_ids){
            //收入
            $team_income = Db::query("select sum(sell_price*num) as total_sell_price from lt_team_income where type=? and parent_id in (".implode(',',$team_ids).")",[$type]);            
            $params['total_sell_price']=floatval($team_income[0]['total_sell_price']?$team_income[0]['total_sell_price']:0);                
            $team_income_ids_arr = Db::query("select id from lt_team_income where parent_id in (".implode(',',$team_ids).")");            
            $team_income_ids=[];
            foreach ($team_income_ids_arr as $v) {
                array_push($team_income_ids,$v['id']);
            }
            if($team_income_ids){
                $team_income_receipt=Db::query("select sum(money) as received from lt_receipt where type=? and income_id in (".implode(',',$team_income_ids).")",[$type]);      
                $params['received']=floatval($team_income_receipt[0]['received']?$team_income_receipt[0]['received']:0);
                $params['no_received'] = floatval($params['total_sell_price']-$params['received']);
            }else{
                $params['received']=0;
                $params['no_received'] =0;
            }

            //支出
            $team_outlay = Db::query("select sum(cost_price*num) as total_cost_price from lt_team_outlay where type=? and parent_id in (".implode(',',$team_ids).")",[$type]);            
            $params['total_cost_price']=floatval($team_outlay[0]['total_cost_price']?$team_outlay[0]['total_cost_price']:0);                
            $team_outlay_ids_arr = Db::query("select id from lt_team_outlay where parent_id in (".implode(',',$team_ids).")");            
            $team_outlay_ids=[];
            foreach ($team_outlay_ids_arr as $v) {
                array_push($team_outlay_ids,$v['id']);
            }
            if($team_outlay_ids){
                $team_outlay_payment=Db::query("select sum(money) as payment from lt_payment where type=? and outlay_id in (".implode(',',$team_outlay_ids).")",[$type]);      
                $params['payment']=floatval($team_outlay_payment[0]['payment']?$team_outlay_payment[0]['payment']:0);
                $params['no_payment'] = floatval($params['total_cost_price']-$params['payment']);
            }else{
                $params['payment']=0;
                $params['no_payment'] = 0;
            }
            $params['profit']=floatval($params['total_sell_price']-$params['total_cost_price']);
            

        }else{
            $params['total_sell_price']=0;
            $params['total_cost_price']=0;              
            $params['received']=0;              
            $params['payment']=0;
            $params['no_received']=0;
            $params['no_payment']=0;
            $params['profit']=0;
        }
        return $params;
    }

    private function cost($type,$date,$branch){
        if($date['departure']==1){
            $sql="booking_date";
        }else{
            $sql="start_date";
        }
        $single_ids_arr = Db::query("select id from lt_single where type=? and {$sql}>=? and {$sql}<=?",[$type,$date['start_date'],$date['end_date']]);            
        $single_ids=[];
        foreach ($single_ids_arr as $v) {
            array_push($single_ids,$v['id']);
        }
        if($single_ids){
            $single = Db::query("select sum((sell_price-pay_price)*num) as total_sell_price,sum((cost_price-pay_price)*num) as total_cost_price from lt_single where id in (".implode(',',$single_ids).")");            
            $params['total_sell_price']=floatval($single[0]['total_sell_price']?$single[0]['total_sell_price']:0);
            $params['total_cost_price']=floatval($single[0]['total_cost_price']?$single[0]['total_cost_price']:0);
            $single_receipt = Db::query("select sum(money) as received from lt_receipt where type=? and income_id in (".implode(',',$single_ids).")",[$type]);
            $params['received']=floatval($single_receipt[0]['received']?$single_receipt[0]['received']:0);
            $single_payment = Db::query("select sum(money) as payment from lt_payment where type=? and outlay_id in (".implode(',',$single_ids).")",[$type]);
            $params['payment']=floatval($single_payment[0]['payment']?$single_payment[0]['payment']:0);
            $params['no_received'] = floatval($params['total_sell_price']-$params['received']);
            $params['no_payment'] = floatval($params['total_cost_price']-$params['payment']);
            $params['profit']=floatval($params['total_sell_price']-$params['total_cost_price']);
        }else{
            $params['total_sell_price']=0;
            $params['total_cost_price']=0;              
            $params['received']=0;              
            $params['payment']=0;
            $params['no_received']=0;
            $params['no_payment']=0;
            $params['profit']=0;
        }
        return $params;
    }

    private function yan($type,$date,$branch){
        if($date['departure']==1){
            $sql="booking_date";
        }else{
            $sql="start_date";
        }
        $single_ids_arr = Db::query("select id from lt_single where type=? and {$sql}>=? and {$sql}<=?",[$type,$date['start_date'],$date['end_date']]);            
        $single_ids=[];
        foreach ($single_ids_arr as $v) {
            array_push($single_ids,$v['id']);
        }
        if($single_ids){
            $single = Db::query("select sum((sell_price-pay_price)*num) as total_sell_price,sum((cost_price-pay_price)*num) as total_cost_price from lt_single where id in (".implode(',',$single_ids).")");            
            $params['total_sell_price']=floatval($single[0]['total_sell_price']?$single[0]['total_sell_price']:0);
            $params['total_cost_price']=floatval($single[0]['total_cost_price']?$single[0]['total_cost_price']:0);
            $single_receipt = Db::query("select sum(money) as received from lt_receipt where type=? and income_id in (".implode(',',$single_ids).")",[$type]);
            $params['received']=floatval($single_receipt[0]['received']?$single_receipt[0]['received']:0);
            $single_payment = Db::query("select sum(money) as payment from lt_payment where type=? and outlay_id in (".implode(',',$single_ids).")",[$type]);
            $params['payment']=floatval($single_payment[0]['payment']?$single_payment[0]['payment']:0);
            $params['no_received'] = floatval($params['total_sell_price']-$params['received']);
            $params['no_payment'] = floatval($params['total_cost_price']-$params['payment']);
            $params['profit']=floatval($params['total_sell_price']-$params['total_cost_price']);
        }else{
            $params['total_sell_price']=0;
            $params['total_cost_price']=0;              
            $params['received']=0;              
            $params['payment']=0;
            $params['no_received']=0;
            $params['no_payment']=0;
            $params['profit']=0;
        }
        return $params;
    }
    
    //导出excel
    public function export_excel(){  
        
        

        $title = array('部门','收入','已收','未收','支出','已付','未付','毛利','收入','已收','未收','支出','已付','未付','毛利','收入','已收','未收','支出','已付','未付','毛利','收入','已收','未收','支出','已付','未付','毛利','收入','已收','未收','支出','已付','未付','毛利','收入','已收','未收','支出','已付','未付','毛利');
        include(EXTEND_PATH.'phpexcel/PHPExcel.php'); 
        $obj = new \PHPExcel();      
        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('C')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('D')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('E')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('F')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('G')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('H')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('I')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('J')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('K')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('L')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('M')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('N')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('O')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('P')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('Q')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('R')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('S')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('T')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('U')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('V')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('W')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('X')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('Y')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('Z')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AA')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AB')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AC')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AD')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AE')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AF')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AG')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AH')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AI')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AJ')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AK')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AL')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AM')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AN')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AO')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AP')->setWidth(18);
        $obj->getActiveSheet()->getColumnDimension('AQ')->setWidth(18);


        $obj->getActiveSheet()->mergeCells('B1:H1');
        $obj->getActiveSheet()->mergeCells('I1:O1');
        $obj->getActiveSheet()->mergeCells('P1:V1');
        $obj->getActiveSheet()->mergeCells('W1:AC1');
        $obj->getActiveSheet()->mergeCells('AD1:AJ1');
        $obj->getActiveSheet()->mergeCells('AK1:AQ1');
        $obj->setActiveSheetIndex(0)->setCellValue("B1", '团队');      
        $obj->setActiveSheetIndex(0)->setCellValue("I1", '散客');  
        $obj->setActiveSheetIndex(0)->setCellValue("P1", '项目');  
        $obj->setActiveSheetIndex(0)->setCellValue("W1", '单项');  
        $obj->setActiveSheetIndex(0)->setCellValue("AD1", '电子票');  
        $obj->setActiveSheetIndex(0)->setCellValue("AK1", '现票');  
        //横向单元格标识      
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
        $obj->getActiveSheet(0)->setTitle('报表');   //设置sheet名称  
        
        $_row = 2;   //设置纵向单元格标识      
        if($title){      
            $_cnt = count($title);    
            $i = 0;      
            foreach($title AS $v){   //设置列标题      
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);      
                $i++;      
            }      
            $_row++;      
        }      
        $params = $this->request->param();          
        $date=[];        

        if(!empty($params['start_date']) && !empty($params['end_date'])){
            $date=[                
                'start_date'=>$params['start_date'],
                'end_date'=>$params['end_date']
            ];
        }else{
            return "缺少日期";
        }  
        if(!empty($params['is_departure']) && $params['is_departure']==1){
            $date['departure']=$params['is_departure'];
        }else{
            $date['departure']=0;
        }
        
        $list = Db::query('select id,title from lt_category where parent_id=9');
        array_push($list,['id'=>0,'title'=>"无部门"]);
        
        $data=[];        
        foreach($list as $item){       
            $params=[];     
            //单项     
            $params['branch']=$item['title']; 
              
            $team=$this->team(1,$date,$item);
            $params['team']=$team;

            $team=$this->team(2,$date,$item);
            $params['scatter']=$team;

            $team=$this->team(3,$date,$item);
            $params['project']=$team;

            $single=$this->single(4,$date,$item);
            $params['single']=$single;

            $single=$this->single(5,$date,$item);
            $params['qrs']=$single;
            
            $single=$this->single(8,$date,$item);
            $params['ticket']=$single;
            
            array_push($data,$params);   
        }   

        //填写数据      
        if($data){      
            $i = 0;      
            foreach($data AS $_v){                
                $obj->getActiveSheet(0)->setCellValue($cellName[0] . ($i+$_row), $_v['branch']);
                $obj->getActiveSheet(0)->setCellValue($cellName[1] . ($i+$_row), $_v['team']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[2] . ($i+$_row), $_v['team']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[3] . ($i+$_row), $_v['team']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[4] . ($i+$_row), $_v['team']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[5] . ($i+$_row), $_v['team']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[6] . ($i+$_row), $_v['team']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[7] . ($i+$_row), $_v['team']['profit']);

                $obj->getActiveSheet(0)->setCellValue($cellName[8] . ($i+$_row), $_v['scatter']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[9] . ($i+$_row), $_v['scatter']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[10] . ($i+$_row), $_v['scatter']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[11] . ($i+$_row), $_v['scatter']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[12] . ($i+$_row), $_v['scatter']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[13] . ($i+$_row), $_v['scatter']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[14] . ($i+$_row), $_v['scatter']['profit']);

                $obj->getActiveSheet(0)->setCellValue($cellName[15] . ($i+$_row), $_v['project']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[16] . ($i+$_row), $_v['project']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[17] . ($i+$_row), $_v['project']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[18] . ($i+$_row), $_v['project']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[19] . ($i+$_row), $_v['project']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[20] . ($i+$_row), $_v['project']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[21] . ($i+$_row), $_v['project']['profit']);

                $obj->getActiveSheet(0)->setCellValue($cellName[22] . ($i+$_row), $_v['single']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[23] . ($i+$_row), $_v['single']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[24] . ($i+$_row), $_v['single']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[25] . ($i+$_row), $_v['single']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[26] . ($i+$_row), $_v['single']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[27] . ($i+$_row), $_v['single']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[28] . ($i+$_row), $_v['single']['profit']);

                $obj->getActiveSheet(0)->setCellValue($cellName[29] . ($i+$_row), $_v['qrs']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[30] . ($i+$_row), $_v['qrs']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[31] . ($i+$_row), $_v['qrs']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[32] . ($i+$_row), $_v['qrs']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[33] . ($i+$_row), $_v['qrs']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[34] . ($i+$_row), $_v['qrs']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[35] . ($i+$_row), $_v['qrs']['profit']);

                $obj->getActiveSheet(0)->setCellValue($cellName[36] . ($i+$_row), $_v['ticket']['total_sell_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[37] . ($i+$_row), $_v['ticket']['received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[38] . ($i+$_row), $_v['ticket']['no_received']);
                $obj->getActiveSheet(0)->setCellValue($cellName[39] . ($i+$_row), $_v['ticket']['total_cost_price']);
                $obj->getActiveSheet(0)->setCellValue($cellName[40] . ($i+$_row), $_v['ticket']['payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[41] . ($i+$_row), $_v['ticket']['no_payment']);
                $obj->getActiveSheet(0)->setCellValue($cellName[42] . ($i+$_row), $_v['ticket']['profit']);            
                            
                $i++;      
            }      
        }  
        //文件名处理      
        
        $fileName = uniqid(time(),true);      
        
        $objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');  
        $isDown=true;
        if($isDown){   //网页下载      
            header('pragma:public');      
            header("Content-Disposition:attachment;filename=$fileName.xls");      
            $objWrite->save('php://output');exit;      
        }      
        $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码      
        $_savePath = $savePath.$_fileName.'.xlsx';      
         $objWrite->save($_savePath);  
         return $savePath.$fileName.'.xlsx';      
    }     
}
